/*
 *  Copyright 2013 National Institute of Advanced Industrial Science and Technology
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.sss.mapreduce.datatype;

/**
 * A Packable class that encapsulates a String object with creation time.
 */
public class TimedPackableString implements Packable {
  private static final long SIZE = Long.SIZE / 8;
  private static final String EMPTY_STRING = new String();
  private String string;
  private long time;
  /**
   * Constructs a newly allocated TimedPackableString object that represents
   * an empty String.
   */
  public TimedPackableString() { this(EMPTY_STRING, 0L); }
  /**
   * Constructs a newly allocated TimedPackableString object that represents
   * the primitive String argument.
   * @param string the value to be represented by the TimedPackableString.
   * @param time creation time of this TimedPackableString object.
   */
  public TimedPackableString(String string, long time) {
    if (string == null) { string = EMPTY_STRING; }
    this.string = string;
    this.time = time;
  }
  /**
   * Constructs a newly allocated TimedPackableString object that represents
   * a primitive String of the PackableString argument.
   * @param packableString the value to be represented by the TimedPackableString.
   * @param time creation time of this TimedPackableString object.
   */
  public TimedPackableString(PackableString packableString, long time) {
    this(packableString.get(), time);
  }
  /**
   * Return the String value of this TimedPackableString object.
   * @return the String value represented by this object
   */
  public String getString() { return this.string; }
  /**
   * Return the time value of this TimedPackableString object.
   * @return the time value represented by this object
   */
  public long getTime() { return this.time; }
  /**
   * Indicates whether some other object is "equal to" this one.
   * see {@link Packable#equals}
   */
  @Override
  public boolean equals(Object o) {
    if (o instanceof TimedPackableString) {
      if (o == this) {
        return true;
      }
      return string.equals(((TimedPackableString) o).getString())
        && time == ((TimedPackableString) o).getTime();
    }
    else {
      return false;
    }
  }
  /**
   * Return a hash code value for this object.
   * see {@link Packable#hashCode}
   */
  @Override
  public int hashCode() {
    return string.hashCode();
  }
  /**
   * Indicates which slot this object should be put.
   *
   * see {@link Packable#slot}
   */
  @Override
  public int slot() {
    return hashCode() & 0x0f;
  }
  @Override
  public long getRoughSize() {
    return 48 + string.length() * 2 + SIZE;
  }
}
